package com.wang.controller.api;

import com.wang.entity.User;
import com.wang.service.CartService;
import com.wang.utils.ConstantUtil;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Classname ApiCartController
 * @Description TODO
 * @Date 2025/3/24 10:46
 * @Created by pc
 */
@RestController
@RequestMapping("/api/cart")
@Api(value = "购物车接口",tags = "ApiCartController")
public class ApiCartController {

    @Resource
    private CartService cartService;

    @GetMapping("/findCartCount")
    @ApiOperation(value = "获取购物车数量",notes = "前台获取购物车数量")
    public ResponseUtil findCartCount(HttpSession session){
        User loginUser = (User) session.getAttribute(ConstantUtil.LOGIN_USER);
        ResponseUtil r = null;
        if(loginUser != null){
             r = cartService.findCount(loginUser.getId());
        }else{
            r = ResponseUtil.get(ResponseEnum.CART_FIND_COUNT_FAILURE);
        }
        return r;
    }

    @GetMapping("/findCart")
    @ApiOperation(value = "获取购物车详情",notes = "前台获取购物车详情")
    public ResponseUtil findCart(HttpSession session){
        User loginUser = (User) session.getAttribute(ConstantUtil.LOGIN_USER);
        ResponseUtil r = null;
        if(loginUser != null){
            r = cartService.findCartByUserId(loginUser.getId());
        }else{
            r = ResponseUtil.get(ResponseEnum.CART_FIND_COUNT_FAILURE);
        }
        return r;
    }

    // 更新购物车状态 参数 id  quantity checked
    @PostMapping("/updateCart")
    @ApiOperation(value = "更新购物车状态",notes = "前台更新购物车状态")
    public ResponseUtil updateCart(@RequestParam("id")Integer id,
                                   @RequestParam("quantity")Integer quantity,
                                   @RequestParam("checked")Integer checked){
        return cartService.updateCart(id,quantity,checked);
    }

}
